import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import java.io.*;
import java.util.Stack;

/**
 * @author Huhao
 * &#064;date  2024/12/31
 */

class SLR1 extends JFrame implements ActionListener{

    JFrame frame;

    JButton b0;
    JPanel p1,p2,p3,p4,p5,p6;
    static JTextArea t1,t2,t3,t4,t5,t6;
    JButton b1,b2,b3,b4;

    JLabel l0,l1,l3,l4,l5,l6;
    Box vbox;

    public SLR1()
    {
        frame = new JFrame();
        frame.setTitle("SLR（1）分析");
        frame.setLocation(300,100);
        frame.setSize(1300,900);

        b0=new JButton("读入文法(S为开始)");
        b3=new JButton("清空");
        b4 =new JButton("形式转换");
        b1=new JButton("开始分析");
        l0=new JLabel("输入字符串：");
        t2=new JTextArea(1,30);
        b2=new JButton("判断");
        l1=new JLabel("输入的文法为：");
        l3=new JLabel("分析表：");

        l4=new JLabel("项目集族：");
        l5=new JLabel("分析句子：");
        l6=new JLabel("四元式序列：");

        t4=new JTextArea(24,30);
        t5=new JTextArea(24,30);
        t6=new JTextArea(4,30);

        vbox = Box.createVerticalBox();

        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();
        p4=new JPanel();
        p5=new JPanel();
        p6=new JPanel();

        t1=new JTextArea(12,20);
        t3=new JTextArea(24,40);

        JScrollPane jp2=new JScrollPane(t2);

        vbox.add(p4);
        vbox.add(p5);
        p4.add(b0);
        p4.add(b4);
        p4.add(b3);
        p4.add(b1);


        p5.add(l0);
        p5.add(jp2);
        p5.add(b2);


        p1.add(vbox);

        frame.add(p1,"North");

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());

        JPanel upperPanel = new JPanel();
        upperPanel.setLayout(new BorderLayout());
        upperPanel.add(l1,BorderLayout.NORTH);
        upperPanel.add(new JScrollPane(t1), BorderLayout.CENTER);

        JPanel lowerPanel = new JPanel();
        lowerPanel.setLayout(new BorderLayout());
        lowerPanel.add(l3,BorderLayout.NORTH);
        lowerPanel.add(new JScrollPane(t3), BorderLayout.CENTER);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        topPanel.add(l4, BorderLayout.NORTH);
        topPanel.add(new JScrollPane(t4), BorderLayout.CENTER);

        JPanel middlePanel = new JPanel();
        middlePanel.setLayout(new BorderLayout());
        middlePanel.add(l5, BorderLayout.NORTH);
        middlePanel.add(new JScrollPane(t5), BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BorderLayout());
        bottomPanel.add(l6, BorderLayout.NORTH);
        bottomPanel.add(new JScrollPane(t6), BorderLayout.CENTER);

        centerPanel.add(upperPanel, BorderLayout.NORTH);
        centerPanel.add(lowerPanel, BorderLayout.CENTER);
        centerPanel.add(topPanel, BorderLayout.WEST);
        centerPanel.add(middlePanel, BorderLayout.EAST);
        centerPanel.add(bottomPanel, BorderLayout.SOUTH);

        frame.add(centerPanel, "Center");

        b0.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }



    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==b0)//读文件
        {
            File getPath;
            JFileChooser jf=new JFileChooser("./");
            FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文档(*.txt)", "txt");
            jf.setFileFilter(filter);
            int value=jf.showOpenDialog(frame);
            if(value==JFileChooser.APPROVE_OPTION){
                getPath=jf.getSelectedFile();
                File fp=getPath;
                BufferedReader reader = null;
                String tempString;
                String sum="";
                try {
                    reader = new BufferedReader(new FileReader(fp));
                    while ((tempString = reader.readLine()) != null) {
                        if(sum.equals(""))
                            sum=sum+tempString;
                        else
                            sum=sum+"\n"+tempString;
                    }
                    this.t1.setText(sum);
                    reader.close();
                } catch (FileNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                } finally{
                    if(reader != null){
                        try {
                            reader.close();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            }
        }
        if(e.getSource()== b4)//形式转换
        {
            String s_ori[]=t1.getText().split("\n");
            String s[]=new String[100];
            t1.setText("");

            int temp=0;
            int flag=0;
            for(int i=0;i< s_ori.length;i++){

                String left = s_ori[i].split("->")[0];//左部
                if(left.equals("S")){
                    flag=1;
                }

                String right = s_ori[i].split("->")[1];//右部
                String rights[] = right.split("\\|");
                for(int j=0;j<rights.length;j++){
                    s[temp]=left+"->"+rights[j];
                    temp++;

                }
            }
            char start = s[0].charAt(0);
            for(int i=0;s[i]!=null;i++){
                if(i==0){
                    t1.append(s[i]);
                }
                else{
                    t1.append("\n");
                    t1.append(s[i]);
                }
            }
        }

        if(e.getSource()==b1)
        {
            String s[]=t1.getText().split("\n");
            SLRAnalyser slrAnalyser = new SLRAnalyser(s);
        }
        if(e.getSource()==b3)
        {
            t1.setText("");
            t2.setText("");
            t3.setText("");
            t4.setText("");
            t5.setText("");
            t6.setText("");
        }
        if(e.getSource()==b2)
        {
            LexicalAnalyser lexicalAnalyser = new LexicalAnalyser("SLR1");
            java.util.List<Token> tokenlist = lexicalAnalyser.analise(SLRAnalyser.G);
            Stack<Token> stack = lexicalAnalyser.convertToStack(SLRAnalyser.G, tokenlist);
            SLRAnalyser.analyse(stack, SLRAnalyser.G, SLRAnalyser.analyserTable, lexicalAnalyser.siyuan);
        }
    }

//    public static void main(String args[]){
//        new SLR1();
//    }
}
